#ifndef ENCRYPT_H
#define ENCRYPT_H

#include <QObject>
#include <QFile>
#include <QDateTime>
#include <QDebug>
#include <QMutex>

#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/aes.h>
#include <stdio.h>
#include <string.h>

#include"System/DataTypeConver.h"


class Encrypt : public QObject
{
    Q_OBJECT
public:

    static Encrypt* getInstance()
    {
        if(!m_pInstance)
        {
            QMutexLocker lock(&m_Mutex);
            if(!m_pInstance)
            {
                Encrypt *pInstance = new Encrypt();
                m_pInstance = pInstance;
            }
        }
        return m_pInstance;
    }

    bool initRsaKey(const QString &path);
    void setAesKey(const QByteArray &key, const QByteArray &ivec);
    void getAesKey(QByteArray &key, QByteArray &ivec);
    QByteArray generateRandom(const int &size);
    QByteArray mixAesKey(const QByteArray &clientKey, const QByteArray &serverKey);

    QByteArray rsaEncrypt(const QByteArray &data);
    QByteArray aesEncrypt(const QByteArray &data);
    QByteArray aesDecrypt(const QByteArray &data);


private:
    explicit Encrypt(QObject *parent = 0);
    ~Encrypt(){}
    Encrypt(const Encrypt &);
    Encrypt &operator=(const Encrypt &);

    static Encrypt* m_pInstance;
    static QMutex m_Mutex;

    RSA *m_pRsaPublicKey;
    QByteArray m_aesKey;
    QByteArray m_aesIvec;
    
};

#endif // CMDENCRYP_H
